﻿using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using SaiLing.Common;
using SaiLing.SingleSignOn.Application.Service.Interfaces.System;
using SaiLing.SingleSignOn.Domain.Entities.System;
using SaiLing.SingleSignOn.Application.Core.Controllers;
using System;
using System.Collections.Generic;
using SaiLing.SingleSignOn.Application.Core.Params;
using System.Linq;
using SaiLing.Domain.Specification;
using SaiLing.SingleSignOn.Application.Core.DataTransferObject.System;
using SaiLing.SingleSignOn.Application.Service;

namespace SaiLing.SingleSignOn.WebApi.Controllers
{
    public class ModuleController : BaseController
    {
        public IModuleService ModuleService { get; set; }

        public IModuleActionService ModuleActionService { get; set; }

        public ModuleController(IModuleService moduleService, IModuleActionService moduleActionService)
        {
            ModuleService = moduleService;
            ModuleActionService = moduleActionService;
        }


        [HttpGet]
        public async Task<ApiJsonResultData> GetByKey(Guid id)
        {
            return await new ApiJsonResultData().RunWithTryAsync(async x =>
            {
                var module = await ModuleService.GetByKey(id);
                module.Actions = ModuleActionService.GetMany(new DirectSpecification<ModuleAction>(cx => cx.LogicRemove == false && cx.ModuleId == module.Id)).OrderBy(y=>y.Sort).ToList();

                x.Data = module;
            });
        }

        [HttpGet]
        public async Task<ApiJsonResultData> List(string name,Guid appid)
        {
            return await new ApiJsonResultData().RunWithTryAsync(async x =>
            {
                var list = await ModuleService.List(name, appid);
             
                x.Data = list;
            });
        }

        [HttpGet]
        public async Task<ApiJsonResultData> PageList(int pageIndex, int pageSize, string name)
        {
            return await new ApiJsonResultData().RunWithTryAsync(async x =>
            {
                var list = await ModuleService.PageList(pageIndex, pageSize, name,Guid.Empty);
                x.Data = list;
            });
        }

        /// <summary>
        /// 获取功能模块列表--树形
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ApiJsonResultData TreeList(Guid appid)
        {
            return new ApiJsonResultData().RunWithTry(x =>
            {
                var modules = ModuleService.GetMany(new DirectSpecification<Module>(m => m.LogicRemove == false && m.AppId == appid)).OrderBy(m => m.Sort).Select(m => new TreeNode()
                { Text = m.Name, Id = m.Id, ParentId = m.ParentId }).ToList().ToTree(new List<TreeNode>(), Guid.Empty, 1);
                var menus = new[] { new TreeNode() { Text = "顶级模块", Id = Guid.Empty } };
                //return menus.Concat(modules.Select(it => new { Text = it.Text, Value = it.Id.ToString() }));
                x.Data = menus.Concat(modules);
            });
        }

        [HttpPost]
        public async Task<ApiJsonResultData> Create([FromForm]Module model, string @return)
        {
            return await new ApiJsonResultData().RunWithTryAsync(async x =>
            {
                await ModuleService.Create(model, LogUser);
                x.AddMessage("创建成功");
                x.RedirectUrl = @return;
            });
        }

        public async Task<ApiJsonResultData> Edit([FromForm]Module model, string @return)
        {
            return await new ApiJsonResultData().RunWithTryAsync(async x =>
            {
                var result = await ModuleService.Edit(model);
                x.AddMessage("修改成功");
                x.Data = result;
                x.RedirectUrl = @return;
            });
        }

        [HttpPost]
        public async Task<ApiJsonResultData> Delete([FromBody]List<RemoveParam> model)
        {
            return await new ApiJsonResultData().RunWithTryAsync(async x =>
            {
                if (model != null && model.Any())
                {
                    var list = new List<Module>();
                    model.ToList().ForEach(f =>
                    {
                        list.Add(new Module { Id = f.Id });
                    });
                    var rulst = await ModuleService.Delete(list.ToArray());
                    x.ReloadPage = true;
                    x.Data = rulst;
                    x.AddMessage("删除成功");
                }
                else
                    x.AddErrorMessage("至少选择一条数据");
            });
        }
    }
}
